<?php
namespace Common\Controller;
class BaseController extends \Think\Controller{
    protected $data = '';


    /**
     * 检测登录
     * @param string $success 成功之后跳转地址
     * @param string $scope 授权方式
     * @param string $type 是否拦截未关注
     */
    protected function check_login($success = '', $scope = '' ,$type=''){
        if (!$_SESSION['openid'] || !$_SESSION['userid']) {
            //检测用户来源入口
            $pk_id = I('pk_id');
            $this -> assign('pk_id',$pk_id);



            if (!$success) $current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            $WeiXin = new \Weixin\Controller\WeixinFuwuController();

            $WeiXin->oauth2_run($current_url, $scope,$type);
        }
    }



    function focus($openid){
        $WeiXin = new \Weixin\Controller\WeixinFuwuController();
        $info = $WeiXin->getUserInfo($openid);
//        return $info['sdgdgh'] > 0 ? true : false;
//        dump($info);
        if($info['subscribe']>0){
            //已经关注
            return true;

        }else{
            if(IS_POST){
                return false;
            }else{
                die(header('location:' . U('Weixin/Remind/index')));
            }
        }
    }



    /**
     * 获取用户信息
     */
//    protected function oauth2_user_info($code){
//        $result = curl_post('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('APPID').'&secret='.C('APP_SECRET').'&code='.$code.'&grant_type=authorization_code');
//        $result = json_decode($result,true);
//        $uInfo =  curl_post('https://api.weixin.qq.com/sns/userinfo?access_token='.$result['access_token'].'&openid='.$result['openid'].'&lang=zh_CN');
//        $uInfo = json_decode($uInfo,true);
//        if($uInfo['errcode'] == 0){
//            return $uInfo;
//        }else{
//            die($uInfo['errmsg']);
//        }
//    }


    /**
     * 返回结果
     * @param type $erorr 错误码
     * @param type $msg 错误描述
     * @param type $model 模型
     */
    protected function rtnResult($erorr,$msg,$model=array()){
        $this->data['error'] = $erorr;
        $this->data['msg'] = $msg;
        if(!empty($model)){
            $erorr == 0 ? $model->commit() : $model->rollback();
        }
        die;
    }


    /**
     * 生成签名
     * Enter description here ...
     */
//    protected function rtnQM(){
//        //获取token
//        $wxKey = S('ticket');
//        if (empty($wxKey)){
//            $wxInfo = curl_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('APPID') .'&secret='.C('APP_SECRET'));
//            $wxInfo = json_decode($wxInfo,true);
//            $token = $wxInfo['access_token'];
//
//            //获取ticket票据
//            $wxKey = curl_get('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi');
//            $wxKey = json_decode($wxKey,true);
//            $wxKey = $wxKey['ticket'];
//            S('ticket',$wxKey,7000);
//        }
//        //生成签名
//        $time = time();
//        $noncestr = mt_rand(100000000, 999999999);
//        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//        $str = 'jsapi_ticket='.$wxKey.'&noncestr='.$noncestr.'&timestamp='.$time.'&url='.$url;
//        $signature = sha1($str);
//
//
//        //存储信息
//        $rtn['signature'] = $signature;
//        $rtn['timestamp'] = $time;
//        $rtn['noncestr'] = $noncestr;
//        return $rtn;
//    }


    /**
     * 显示视图
     * @param string $data 要赋值的数据
     * @param string $file 输出的模板文件名
     */
    protected function showView($data='',$file=''){
//        $data['wxKey'] = $this->rtnQM();
        //模板输出
        if (!empty($data) && is_array($data)){
            foreach ($data as $key => $value){
                $this->assign($key,$value);
            }
        }
        $this->display($file);
    }


    /**
     * 获取当前可用次数
     * @return int|mixed
     */
    function getPlayNumber(){
        !$_SESSION['userid'] && die('not found uid');
        $uid = $_SESSION['userid'];

        $up_time = S('clean_time');
        if(empty($up_time)){
            S('clean_time',date('Y-m-d'));
        }else{
            $nowtime = date('Y-m-d');
            if($nowtime != $up_time) { //不是当天
                S('number'.$uid,null);
                S('clean_time',date('Y-m-d'));

            }
        }

        $play = S('number'.$uid);

        if(!$play){

            $map['create_time'] = array('like',date('Y-m-d').'%');//是今天，如何判断
            $map['fk_user_id'] = $_SESSION['userid'];

            $play = M('vote')->where($map)->count();
            $play = C('FREE_NUM') - $play;
            $play < 0 && $play = 0;
            S('number'.$uid,$play);
        }

        return $play;
    }



    /**
     * 降低可用次数
     */
    function decNumber($number){
//        $number = S('number_'.$_SESSION['userid']);
//        $number = $this->getPlayNumber();
        !$_SESSION['userid'] && die('not found uid');
        $uid = $_SESSION['userid'];
//        $way = S('way_'.$uid);
        S('number'.$uid , $number-1);
//        if(S('number'.$uid)<0){
//            S('number'.$uid)==0;
//        }
    }


    function __destruct() {
        if(!empty($this->data)){
            $this->ajaxReturn($this->data);
        }
    }
    
}

